package com.huawei.appmarket.service.alarm.control;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.secure.android.common.intent.SafeBundle;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import o.cdb;
import o.ceq;
import o.dhq;
import o.Logger;
import o.dpn;
import o.dpw;
import o.dsi;
import o.edn;
import o.CollectCheck;
import o.ett;
import o.euc;

public class RepeatingTaskManager extends Service {
    public static final String KEY_PRE_DOWNLOAD_START_TYPE = "startType";
    protected static final String RUN_TASK_EXTRA = "runTaskExtra";
    protected static final String RUN_TASK_ORDER = "runTaskOrder";
    protected static final String RUN_TASK_TYPE = "runTaskType";
    private static final Map<Integer, List<Class<? extends dpn<?, ?>>>> ˋ = new HashMap();
    private volatile ServiceHandler ˊ;
    private final IBinder ˎ = new d(this);
    private final AtomicInteger ˏ = new AtomicInteger();
    private volatile Looper ॱ;

    final class ServiceHandler extends Handler {
        final /* synthetic */ RepeatingTaskManager ˏ;

        public ServiceHandler(RepeatingTaskManager repeatingTaskManager, Looper looper) {
            this.ˏ = repeatingTaskManager;
            super(looper);
        }

        public void handleMessage(Message message) {
            if (message.obj instanceof Intent) {
                this.ˏ.ˎ((Intent) message.obj);
            }
            if (this.ˏ.releaseService() <= 0) {
                new Handler(this.ˏ.getMainLooper()).post(new Runnable(this) {
                    final /* synthetic */ ServiceHandler ˎ;

                    {
                        this.ˎ = r1;
                    }

                    public void run() {
                        if (this.ˎ.ˏ.getRefCount() <= 0) {
                            dpw.ˏ().ॱ();
                        }
                    }
                });
            }
        }
    }

    public class d extends Binder {
        final /* synthetic */ RepeatingTaskManager ॱ;

        public d(RepeatingTaskManager repeatingTaskManager) {
            this.ॱ = repeatingTaskManager;
        }

        public RepeatingTaskManager ˏ() {
            return this.ॱ;
        }
    }

    public static void addJobTask(int i, Class<? extends dpn<?, ?>> cls) {
        List list = (List) ˋ.get(Integer.valueOf(i));
        if (list == null) {
            list = new ArrayList();
            ˋ.put(Integer.valueOf(i), list);
        }
        if (!list.contains(cls)) {
            list.add(cls);
            if (((Integer) dpn.ॱ.get(cls)) == null) {
                dpn.ॱ.put(cls, Integer.valueOf(1 << dpn.ॱ.size()));
            }
        }
    }

    public static void executeJobTasks(Context context, Bundle bundle, int i) {
        int i2 = 0;
        ArrayList arrayList = new ArrayList();
        List<Class> list = (List) ˋ.get(Integer.valueOf(i));
        if (CollectCheck.isEmpty((List) list)) {
            Logger.ˎ("RepTaskHandler", "job list is empty, can not execute tasks, job id: " + i);
            return;
        }
        for (Class cls : list) {
            Integer num = (Integer) dpn.ॱ.get(cls);
            if (num != null) {
                i2 |= num.intValue();
                arrayList.add(num);
            }
            i2 = i2;
        }
        ˎ(context, bundle, i2, arrayList);
    }

    public static void execute(Context context, Class<? extends dpn<?, ?>>... clsArr) {
        execute(context, null, clsArr);
    }

    public static void execute(Context context, Bundle bundle, Class<? extends dpn<?, ?>>... clsArr) {
        ArrayList arrayList = new ArrayList();
        int i = 0;
        for (Object obj : clsArr) {
            Integer num = (Integer) dpn.ॱ.get(obj);
            if (num == null) {
                int size = 1 << dpn.ॱ.size();
                dpn.ॱ.put(obj, Integer.valueOf(size));
                num = Integer.valueOf(size);
            }
            i |= num.intValue();
            arrayList.add(num);
        }
        ˎ(context, bundle, i, arrayList);
    }

    private static void ˎ(final Context context, final Bundle bundle, final int i, final ArrayList<Integer> arrayList) {
        dsi.ˏ(new AsyncTask() {
            protected Object doInBackground(Object[] objArr) {
                final Intent intent = new Intent(context, RepeatingTaskManager.class);
                intent.putExtra(RepeatingTaskManager.RUN_TASK_TYPE, i);
                intent.putIntegerArrayListExtra(RepeatingTaskManager.RUN_TASK_ORDER, arrayList);
                intent.putExtra(RepeatingTaskManager.RUN_TASK_EXTRA, bundle);
                dpw.ˏ().ˏ(new Runnable(this) {
                    final /* synthetic */ AnonymousClass3 ॱ;

                    public void run() {
                        if (dpw.ˏ().ˎ != null) {
                            dpw.ˏ().ˎ.handleTask(intent);
                        } else {
                            Logger.e("RepTaskHandler", "mRepeatingTaskService is null");
                        }
                    }
                });
                return null;
            }
        });
    }

    private void ˎ(Intent intent) {
        if (intent != null) {
            euc.ˊ().ॱ();
            SafeIntent safeIntent = new SafeIntent(intent);
            int intExtra = safeIntent.getIntExtra(RUN_TASK_TYPE, 0);
            ArrayList integerArrayListExtra = safeIntent.getIntegerArrayListExtra(RUN_TASK_ORDER);
            Bundle bundleExtra = safeIntent.getBundleExtra(RUN_TASK_EXTRA);
            if (integerArrayListExtra == null) {
                Logger.e("ScheduleRepeatService", "RepTaskHandlercan not find order.");
                euc.ˊ().ˏ();
                return;
            }
            Logger.ˎ("ScheduleRepeatService", "RepTaskHandler" + " begin repeating task, time:" + System.currentTimeMillis() + ",runTasks:" + Integer.toBinaryString(intExtra) + ",orderList:" + integerArrayListExtra.toString());
            ˎ();
            Iterator it = integerArrayListExtra.iterator();
            while (it.hasNext()) {
                Integer num = (Integer) it.next();
                if (!dhq.ॱ().ˎ()) {
                    break;
                }
                ˏ(num, new SafeBundle(bundleExtra));
            }
            euc.ˊ().ˏ();
            Logger.ˎ("ScheduleRepeatService", "RepTaskHandler end repeating task!!!time:" + System.currentTimeMillis());
        }
    }

    public void onCreate() {
        super.onCreate();
        HandlerThread handlerThread = new HandlerThread("RepTaskHandler");
        handlerThread.start();
        this.ॱ = handlerThread.getLooper();
        this.ˊ = new ServiceHandler(this, this.ॱ);
        edn.ˋ().ˊ(System.currentTimeMillis());
        ett.ˏ().ॱ("RepeatingTask");
    }

    public void handleTask(@NonNull Intent intent) {
        try {
            Message obtainMessage = this.ˊ.obtainMessage(1);
            obtainMessage.obj = intent;
            obtainMessage.what = 1;
            this.ˊ.sendMessage(obtainMessage);
        } catch (Throwable e) {
            Logger.ˋ("RepTaskHandler", "handleTask", e);
        }
    }

    @Nullable
    public IBinder onBind(Intent intent) {
        return this.ˎ;
    }

    public void onDestroy() {
        super.onDestroy();
        Logger.ˎ("RepTaskHandler", "onDestroy");
        this.ˏ.set(0);
        this.ॱ.quit();
        ett.ˏ().ˊ();
    }

    private void ˏ(Integer num, SafeBundle safeBundle) {
        if (num != null) {
            for (Entry entry : dpn.ॱ.entrySet()) {
                Object obj = (entry == null || num.intValue() != ((Integer) entry.getValue()).intValue()) ? null : 1;
                if (obj != null) {
                    dpn ˋ = ˋ(entry);
                    if (ˋ != null) {
                        try {
                            ˋ.ˋ(safeBundle);
                            ˋ.ˊ(getApplicationContext());
                        } catch (InterruptedException e) {
                            Logger.ˋ("RepTaskHandler", "execute task InterruptedException!!" + e.toString());
                            return;
                        } catch (Exception e2) {
                            Logger.ˋ("RepTaskHandler", "execute task Exception!!" + e2.toString());
                        }
                    } else {
                        continue;
                    }
                }
            }
        }
    }

    @Nullable
    private dpn<?, ?> ˋ(@NonNull Entry<Class<? extends dpn<?, ?>>, Integer> entry) {
        try {
            return (dpn) ((Class) entry.getKey()).newInstance();
        } catch (InstantiationException e) {
            Logger.ˋ("RepTaskHandler", "newInstance InstantiationException!!" + e.toString());
            return null;
        } catch (IllegalAccessException e2) {
            Logger.ˋ("RepTaskHandler", "newInstance IllegalAccessException!!" + e2.toString());
            return null;
        } catch (Exception e3) {
            Logger.ˋ("RepTaskHandler", "instance task Exception!!" + e3.toString());
            return null;
        }
    }

    private void ˎ() {
        long ॱ = ((ceq) cdb.ॱ(ceq.class)).ॱ();
        long j = 180000;
        while (j > 0 && ॱ > 0) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                if (Logger.ˎ()) {
                    Logger.ˏ("RepTaskHandler", "wait for packageservice InterruptedException");
                }
            }
            j -= 5000;
            if (Logger.ˎ()) {
                Logger.ˏ("RepTaskHandler", "wait for PackageService TASK empty:" + j);
            }
        }
    }

    public void acquireService() {
        this.ˏ.incrementAndGet();
    }

    public void acquireService(int i) {
        this.ˏ.addAndGet(i);
    }

    public int releaseService() {
        return this.ˏ.decrementAndGet();
    }

    public int getRefCount() {
        return this.ˏ.get();
    }
}
